package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;


    @RequiredPermission(name = "活动页面", expremission = "activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        Employee user = UserContext.getCurrentUser();
        model.addAttribute("user", user);
        return "activity/list";
    }


    @RequiredPermission(name = "活动删除", expremission = "activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            activityService.delete(id);
        }
        return "activity/input";
    }


    @RequiredPermission(name = "活动新增/编辑", expremission = "activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Activity activity, Long id) {

        //通过插入中间表东西放进去  一个员工的id . 一个你选择的旅游项目的id   ,一个事件
        activityService.insertRelation(UserContext.getCurrentUser().getId(), id, new Date());
        activityService.updateNum(id);

        return new JsonResult();
    }

    @RequestMapping("/result")
    public String result(Long id, Model model) {


        Activity activity = new Activity();
        //查出总人数
        Integer num = activityService.selectNum(id);
        for (long i = 1; i <= 4; i++) {
            Integer a = activityService.selectByNumtoId(1l) / num * 100;
            model.addAttribute("meannum",a);

        }
      //  model.addAttribute("meannum",activity.getMeanNum());
        List<Activity> items = activityService.selectByInId(id);
        model.addAttribute("items", items);

        /*   int  everyNum1= activityService.selectByNumtoId(1l);*/
        //  String a= String.valueOf(everyNum1/num*100);

        //把你的平均值设置回去

        return "activity/result";
    }

    @RequiredPermission(name = "活动新增/编辑", expremission = "activity:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("activity", activityService.get(id));
            List<Activity> items = activityService.selectByActivityId(id);
            model.addAttribute("items", items);
        }
        return "activity/input";
    }
}
